<template>
  <el-table
    :data="activityList"
    style="width: 100%"
    :header-cell-style="{ 'text-align': 'center' }"
    :cell-style="{ 'text-align': 'center' }"
  >
    <el-table-column prop="activity_id" label="活动编码" />
    <el-table-column prop="activity_name" label="活动名称" />
    <el-table-column prop="activity_category" label="类别">
      <template #default="scope">
        <span v-for="c in categoryList" :key="c.activity_id">
          <span v-if="scope.row.activity_category == c.activity_categoryId">{{
            c.activity_categoryName
          }}</span>
        </span>
      </template>
    </el-table-column>
    <el-table-column prop="activity_startTime" label="开启时间">
      <template #default="scope">
        <span v-if="scope.row.activity_startTime != null">
          {{
            dayjs(scope.row.activity_startTime).format('YYYY-MM-DD HH:mm:ss')
          }}
        </span>
      </template>
    </el-table-column>
    <el-table-column prop="activity_endTime" label="结束时间">
      <template #default="scope">
        <span v-if="scope.row.activity_endTime != null">
          {{ dayjs(scope.row.activity_endTime).format('YYYY-MM-DD HH:mm:ss') }}
        </span>
      </template>
    </el-table-column>
    <el-table-column prop="activity_remarks" label="备注" />
    <el-table-column label="状态" width="200px">
      <template #default="scope">
        <el-button
          type="success"
          size="small"
          v-if="scope.row.activity_state === '1'"
          @click="changeState(scope.row, '2')"
          >开启</el-button
        >
        <el-button
          type="warning"
          size="small"
          v-if="scope.row.activity_state === '2'"
          @click="changeState(scope.row, '1')"
          >关闭</el-button
        >
        <el-button type="primary" size="small" @click="handleMod(scope.row)"
          >修改</el-button
        >
        <el-button type="danger" size="small" @click="handleDel(scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="container-pagination">
    <el-pagination
      v-model:currentPage="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[1, 2, 3, 4, 5, 6, 7]"
      layout="total, sizes, prev, pager, next, jumper"
    />
  </div>
  <ActivityMod ref="activityMod"></ActivityMod>
</template>

<script setup>
import { getCurrentInstance, computed } from 'vue'
import { useStore } from 'vuex'
import useGetters from '@/hooks/useGetters'
import dayjs from 'dayjs'
import ActivityMod from './ActivityMod'
import { reqGetCategoryList } from '@/api/activity'

const store = useStore()
const { dispatch, commit, getters } = store
const poxy = getCurrentInstance()
const activityGettets = useGetters(['activityPagination', 'activityList'])
const { activityPagination: pagination, activityList } = activityGettets
// 改变状态
const changeState = async (activity, state) => {
  await dispatch('activity/updateActivity', {
    activity_id: activity.activity_id,
    activity_state: state
  }).then(() => {
    getActivityList()
  })
}
// 修改按钮
const handleMod = async (activity) => {
  commit('activity/SAVEACTIVITY', activity)
  poxy.refs.activityMod.changeDialogFormVisible()
}
// 删除按钮
const handleDel = async (activity) => {
  await dispatch('activity/updateActivity', {
    activity_id: activity.activity_id,
    activity_status: 0
  }).then(() => {
    getActivityList()
  })
}
// 每页数量改变的方法
const handleSizeChange = (val) => {
  pagination.value.pageSize = val
  getActivityList()
}
// 页数改变的方法
const handleCurrentChange = (val) => {
  pagination.value.currentPage = val
  getActivityList()
}
// 初始化
const getActivityList = () => {
  dispatch('activity/getActivityList', { pagination })
}
getActivityList()
const categoryList = computed(() => getters.categoryList)
const getCategoryList = async () => {
  const result = await reqGetCategoryList({ currentPage: 1, pageSize: 500 })
  if (result.code === 200) {
    commit('activity/GETCATEGORYLIST', result.data.data)
  }
}
getCategoryList()
</script>

<style lang="scss" scoped></style>
